package com.yiqi.liebang.feature.order.view;


import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.rxbus2.RxBus;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.entity.bo.OrderBo;
import com.yiqi.liebang.entity.bo.TopicOrderBo;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.order.contract.TopicContract;
import com.yiqi.liebang.feature.order.dependencies.topic.DaggerTopicOrderComponent;
import com.yiqi.liebang.feature.order.dependencies.topic.TopicOrderPresenterModule;
import com.yiqi.liebang.feature.order.view.adapter.QuestionAdapter;
import com.yiqi.liebang.feature.order.view.adapter.TopicAdapter;
import com.yiqi.liebang.framework.DataServer;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;


/**
 * A simple {@link Fragment} subclass.
 */
public class TopicListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, TopicContract.View, BaseQuickAdapter.RequestLoadMoreListener, OnRefreshListener, BaseQuickAdapter.OnItemChildClickListener {

    @Inject
    TopicContract.Presenter mPresenter;

    @BindView(R.id.rv_question)
    RecyclerView mRvTopic;

    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;

    private int mType = 0;

    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<TopicOrderBo> mData;

    private TopicAdapter mAdapter;

    public static TopicListFragment getInstance(int title) {
        TopicListFragment sf = new TopicListFragment();
        sf.mType = title;
        return sf;
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_order_question;
    }

    @Override
    protected void initInjector() {
        DaggerTopicOrderComponent.builder().topicOrderPresenterModule(new TopicOrderPresenterModule(this)).build().inject(this);
    }


    @Override
    protected void initView() {
        initAdapter();

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void onStart() {
        super.onStart();
        pageIndex = 1;
        mPresenter.getTopicOrderList(mType, pageIndex, pageSize);
        RxBus.getDefault().register(this);
        RxBus.getDefault().post(new EventEntity(12));
    }

    public void initAdapter() {
        mSmartRefesh.setEnableLoadMore(false);
        mSmartRefesh.setOnRefreshListener(this);
        mEmptyView = new EmptyView(mRvTopic);
        mRvTopic.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvTopic.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new TopicAdapter(mType);
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.bindToRecyclerView(mRvTopic);
        mAdapter.setOnLoadMoreListener(this, mRvTopic);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void showTopicOrderListSuccess(List<TopicOrderBo> workBoPageData) {
        mSmartRefesh.finishRefresh();
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showTopicOrderListError(String error) {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
        mSmartRefesh.finishRefresh();
    }

    @Override
    public void showTopicOrderListMoreSuccess(List<TopicOrderBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showTopicOrderListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showOrderListEmpty() {
        if (mSmartRefesh != null) mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), OrderInfoActivity.class);
        intent.putExtra("id", mData.get(position).getId());
        intent.putExtra("is_expert", mType==4?true:false);
        startActivity(intent);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refresh();
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    private void refresh() {
        mAdapter.setEnableLoadMore(false);//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageIndex = 1;
        mPresenter.getTopicOrderList(mType, pageIndex, pageSize);
    }

    private void loadMore() {
        mPresenter.getTopicOrderListMore(mType, pageIndex, pageSize);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        RxBus.getDefault().unregister(this);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), UserInfoActivity.class);
        intent.putExtra("is_view_my", false);
        intent.putExtra("visitorUid", mData.get(position).getTopicFormMap().getUserAccountFormMap().getUserUid());
        startActivity(intent);
    }
}
